package com.bantang.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Producer {

    private final static String QUEUE_NAME = "hello2";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.56.101");

        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            //队列名  队列持久化 是否共享多个消费者消费 其他
            channel.queueDeclare(QUEUE_NAME, true, false, false, null);

            Scanner input = new Scanner(System.in);
            while(input.hasNext()){
                String message = input.next();
                channel.basicPublish("",QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes(StandardCharsets.UTF_8));
                //交换机名 路由 其他参数信息 消息体
                System.out.println(" [x] Sent '" + message + "'");
            }

        }
    }
}